<template>
    <div>
        <Table ref="dataTable" stripe :loading="table.loading" :columns="table.tableColumns" :data="table.tableRows"
               style="margin-top:20px;" @on-select="select" @on-select-cancel="selectCancel"
               @on-select-all="selectAll" @on-select-all-cancel="selectAllCancel" @on-sort-change="changeSort"></Table>
        <div style="margin: 20px; overflow: hidden">
            <div style="float: right;">
                <Page :total="pager.total" :current="pager.pageNo" @on-change="changePageNo" @on-page-size-change="changePageSize"
                      showSizer showTotal></Page>
            </div>
        </div>
    </div>
</template>

<script>
    import * as utils from '@/api/utils-v2'
    import headImg from '@/assets/images/head.png'
    import config from '@/config'
    const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
    const cdnUrl = config.baseUrl.cdnUrl

    export default {
        name: 'TopDistributionTable',
        data() {
            return {
                urls: {
                    searchUrl: '/distribution/admin/all-top'
                },
                pager: {
                    pageNo: 1,
                    pageSize: 10,
                    total: 0
                },
                searchOpts: {
                    sortColumn: null,
                    sortOrder: null,
                },
                table: {
          loading: false,
          tableColumns: [
            {
              type: 'selection',
              width: 52,
              key: "id",
              align: 'center',
              fixed: 'left'
            },
            {
              width: 60,
              align: 'center',
              fixed: "left",
              render: (h, params) => {
                return h('span', params.index + (this.pager.pageNo - 1) * this.pager.pageSize + 1)
              }
            },
            {
title: '用户编号',
key: 'userId',
minWidth: 120,
sortable: true,
render: (h, params) => {
						  return h(
						    'Dropdown',
						    {
						      on: {
						        'on-click': itemName => {
						          this.userOpt(itemName, params.row)
						        }
						      },
						      props: {
						        transfer: true
						      }
						    },
						    [
						      h('span', [
						        params.row.userId,
						        h('Icon', {
						          props: {
						            type: 'ios-list',
						            size: '25'
						          }
						        })
						      ]),
						      h(
						        'DropdownMenu',
						        {
						          slot: 'list'
						        },
						        [
						          h(
						            'DropdownItem',
						            {
						              props: {
						                name: 'moduleDetail'
						              }
						            },
						            '详情'
						          ),
						          h(
						            'DropdownItem',
						            {
						              props: {
						                name: 'moduleSearch'
						              }
						            },
						            '搜索'
						          )
						        ]
						      )
						    ]
						  )}
},
{
title: '手机号',
key: 'phone',
minWidth: 120,
sortable: true
},
{
title: '用户邮箱',
key: 'email',
minWidth: 120,
sortable: true
},
{
title: '昵称',
key: 'nickname',
minWidth: 120,
sortable: true
},
{
title: '头像地址',
key: 'headicon',
minWidth: 120,
sortable: true,
render: (h, params) => {
              let imgSrc = params.row.headicon
              if (!imgSrc) {
                imgSrc = headImg
              } else {
                if (imgSrc.indexOf('http') < 0) {
                  imgSrc = cdnUrl + '/' + imgSrc
                }
              }
              return h(
                'img',
                {
                  attrs: {
                    src: imgSrc
                  },
                  style: {
                    width: '60px',
                    height: '60px',
                    borderRadius: '50%'
                  }
                },
                ''
              )
            }
},
{
title: '创建时间',
key: 'createTime',
minWidth: 120,
sortable: true
},
            {
            title: '操作',
            key: 'action',
            width: 130,
            align: 'center',
            fixed: 'right',
            render: (h, params) => {
              return h(
                'Dropdown',
                {
                  on: {
                    'on-click': itemName => {
                      this.userOpt(itemName, params.row)
                    }
                  },
                  props: {
                    transfer: true
                  }
                },
                [
                  h(
                    'Button',
                    {
                      props: {
                        type: 'primary',
                        size: 'small'
                      }
                    },
                    [
                      '选择操作 ',
                      h('Icon', {
                        props: {
                          type: 'ios-arrow-down'
                        }
                      })
                    ]
                  ),
                  h(
                    'DropdownMenu',
                    {
                      slot: 'list'
                    },
                    [
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDetail'
                          }
                        },
                        '详情'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'showDistributionBelow'
                          }
                        },
                        '查看分销等级'
                      ),
                      h(
                        'DropdownItem',
                        {
                          props: {
                            name: 'performanceStat'
                          }
                        },
                        '业绩统计'
                      )
                    ]
                  )
                ]
              )
            }
          }
          ],
          tableDetails: [],
          selections: []
        }
            }
        },
        computed: {},
        mounted() {
            this.search()
        },
        methods: {
            search() {
                this.$emit('searchTable')
            },
            userOpt(itemName, row) {
              if (itemName === 'showDetail') {
                this.$emit('showDetailModal', JSON.parse(JSON.stringify(row)))
              } else if (itemName === 'showDistributionBelow') {
                this.$emit('showDistributionBelow', row)
              } else if (itemName === 'performanceStat') {
                this.$emit('showPerformanceStat', row)
              } else if (itemName === 'moduleDetail') {
                  this.$emit('showModuleDetail', row.userId)
              } else if (itemName === 'moduleSearch') {
                  this.$emit('showModuleSearch')
              }  
            },
            showDetail(row) {
                
            },
            select(selections, row) {
              utils.select(this, selections, row)
            },
            selectCancel(selections, row) {
              utils.selectCancel(this, selections, row)
            },
            selectAll(selections) {
              utils.selectAll(this, selections)
            },
            selectAllCancel(selections) {
              utils.selectAllCancel(this, selections)
            },
            changeSort(sortColumn) {
                utils.changeSort(this, sortColumn)
            },
            changePageNo(pageNo) {
                utils.changePageNo(this, pageNo)
            },
            changePageSize(pageSize) {
                utils.changePageSize(this, pageSize)
            }
        }
    }
</script>

<style>
</style>
